<template>
	<view class="fui-wrap">
		<fui-form error-position="1" labelColor="#7a7a7a" ref="form" top="0" :model="formData">
			<fui-form-item label="自营商户" asterisk prop="merch_name">
				<fui-input readonly :borderBottom="false" :padding="[0]" placeholder="请输入车牌号" v-model="formData.merch_name"></fui-input>
			</fui-form-item>
			<fui-form-item label="车牌号" arrow asterisk prop="plate_number">
				<fui-input readonly @click="$navigateTo('parking/plate?type=input&plate_number='+formData.plate_number)" :borderBottom="false" :padding="[0]" placeholder="请输入车牌号" v-model="formData.plate_number"></fui-input>
			</fui-form-item>
			<fui-form-item label="停车券" arrow asterisk prop="coupon_id">
				<fui-input readonly @click="showCoupon=true" :borderBottom="false" :padding="[0]" placeholder="请选择停车券" :value="formData.coupon_txt"></fui-input>
			</fui-form-item>
			<view class="fui-btn__box">
				<fui-button :disabled="disabledButton" text="提交" bold @click="submit"></fui-button>
			</view>
		</fui-form>
		<fui-picker v-if="coupon" :fields ="['title','id']" :layer="1" :linkage="true" :options="coupon" :show="showCoupon" @change="changePicker" @cancel="cancelPicker"></fui-picker>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	export default {
		data() {
			return {
				coupon:'',
				showCoupon:false,
				formData:{
					merch_id:'',
					merch_name:'',
					plate_number:'',
					coupon_id:'',
					coupon_txt:''
				},
				disabledButton:false
			}
		},
		onShow() {
			let plate_number=uni.getStorageSync('plate_number') || '';
			if(plate_number){
				this.formData.plate_number=plate_number;
				uni.setStorageSync('plate_number','');
			}
		},
		onLoad(e) {
			this.getCoupon(e.merch_id);
		},
		methods: {
			...methods,
			getCoupon:function(merch_id){
				this.$get('parking/merch/get-coupon',{id:merch_id}).then(res=>{
					this.coupon=res.coupon;
					this.formData.merch_id=res.merch.id;
					this.formData.merch_name=res.merch.merch_name;
					if(res.coupon.length>0){
						this.formData.coupon_id=res.coupon[0].id;
						this.formData.coupon_txt=res.coupon[0].title;
					}
				});
			},
			changePicker:function(e){
				if(this.showCoupon){
					this.formData.coupon_id=e.value;
					this.formData.coupon_txt=e.text;
					this.showCoupon=false;
				}
			},
			cancelPicker:function(){
				this.showCoupon=false;
			},
			submit:function(){
				if(!this.formData.plate_number){
					this.$msg('请输入车牌号');
					return;
				}
				this.disabledButton=true;
				this.$post('parking/merch/coupon-send',this.formData,true,true).then(res=>{
					this.$navigateBack(1500);
				}).catch(err=>{
					this.disabledButton=false;
				});
			},
			
		}
	}
</script>

<style lang="scss" scoped>
.fui-btn__box{
	padding: 20rpx;
}
</style>